<template>
  <a-modal
    title="设置常用流程"
    :width="800"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <template v-for="(items,key) in processDataByBusinessPlate">
      <a-card class="card" size="small" :title="filterDictTextByCache(key)" style="margin-top:10px;" :key="key">
        <a-checkbox-group v-model="checkedValues[key]" style="width: 100%;">
          <a-row>
            <template v-for="(item,index) in items">
              <a-col :key="index" :span="6">
                <a-checkbox :value="item.key">{{item.name}}</a-checkbox>
              </a-col>
            </template>
          </a-row>
        </a-checkbox-group>
      </a-card>
    </template>
  </a-modal>
</template>
<script>
import Vue from 'vue'
import { getAction } from '@/api/manage'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
export default {
  name: 'OftenProcessConfig',
  props: {},
  data() {
    return {
      visible: false,
      processData: [],
      processDataByBusinessPlate: {},
      url: {
        list: '/activiti/models/listAll'
      },
      checkedValues: {} // 工单按业务分组，设置的常用时用业务分类做key值
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getProcessSettings()
      }
    }
  },
  methods: {
    loadData() {
      getAction(this.url.list, { isShow: true }).then((res) => { // 查询已有流程
        if (res.success) {
          this.processData = res.result
          this.processDataByBusinessPlate = this.setProcessDataByBusinessPlate()
        } else {
          this.$message.warning(res.message)
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    setProcessDataByBusinessPlate() { // 按业务分类 方便渲染展示
      var list = {}
      this.processData.forEach(function(item, i) {
        if (!list[item.businessPlate]) {
          list[item.businessPlate] = []
        }
        list[item.businessPlate].push(item)
      })
      return list
    },
    handleOk() {
      this.visible = false
      var key = this.$route.name + ':processSettings'
      Vue.ls.set(key, this.checkedValues, 7 * 24 * 60 * 60 * 1000)
      this.$emit('ok')
    },
    handleCancel() {
      this.visible = false
    },
    filterDictTextByCache(text) {
      return filterDictTextByCache('business_sector', text)
    },
    getProcessSettings() {
      var key = this.$route.name + ':processSettings'
      this.checkedValues = Vue.ls.get(key) || {}
    }
  },
  mounted() {
    this.loadData()
  }
}
</script>
<style lang="less">

</style>
